package fr.generali.accueilclient.gwt.client.ui.components.menu.contextual;

import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.generali.accueilclient.gwt.client.ui.components.menu.Resources;

/**
 * Un menu contextuel est une popup qui peut recevoir des
 * {@link ContextualMenuItem}.<br>
 */
public class ContextualMenu extends PopupPanel implements HasMouseOverHandlers, HasMouseOutHandlers {

    private static ContextualMenuUiBinder uiBinder = GWT.create(ContextualMenuUiBinder.class);

    interface ContextualMenuUiBinder extends UiBinder<Widget, ContextualMenu> {
    }

    @UiField
    Resources res;

    @UiField
    FlowPanel menuItemsContainer;

    @UiField
    FocusPanel closeMenu;

    public ContextualMenu() {
        setWidget(uiBinder.createAndBindUi(this));
        res.style().ensureInjected();
    }

    @Override
    public void add(Widget widget) {
        if (!iterator().hasNext()) {
            ((ContextualMenuItem ) widget).setBorderVisible(true);
        }
        menuItemsContainer.remove(closeMenu);
        menuItemsContainer.add(widget);
        menuItemsContainer.add(closeMenu);
    }

    @Override
    public void clear() {
        menuItemsContainer.clear();
    }

    @Override
    public Iterator<Widget> iterator() {
        return menuItemsContainer.iterator();
    }

    @Override
    public boolean remove(Widget widget) {
        return menuItemsContainer.remove(widget);
    }

    @Override
    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return addDomHandler(handler, MouseOverEvent.getType());
    }

    @Override
    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
        return addDomHandler(handler, MouseOutEvent.getType());
    }

    public void addItem(ContextualMenuItem menuItem) {
        menuItemsContainer.add(menuItem);
    }

    public void removeItem(ContextualMenuItem menuItem) {
        menuItemsContainer.remove(menuItem);
    }

    public void setItemEnabled(ContextualMenuItem menuItem, boolean enabled) {
        int widgetIndex = menuItemsContainer.getWidgetIndex(menuItem);
        if (widgetIndex != -1) {
            ((ContextualMenuItem ) menuItemsContainer.getWidget(widgetIndex)).setEnabled(enabled);
        }
    }

    @UiHandler("closeMenu")
    public void closeClicked(ClickEvent event) {
        this.hide();
    }
}
